package com.icetec.iceintel.ui.adapter;


import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.icetec.R;
import com.icetec.iceintel.framework.dragGridView.DragAdapter;
import com.icetec.iceintel.framework.dynamicgrid.DynamicGridUtils;

import java.util.ArrayList;
import java.util.List;

import com.icetec.iceintel.database.GroupControlSqlHelper;
import com.icetec.iceintel.database.SchemeControlSqlHelper;
import com.icetec.iceintel.framework.AppApplication;
import com.icetec.iceintel.netrequest.UniResAllocator;
import com.icetec.iceintel.model.Scheme;
import com.icetec.iceintel.netrequest.InstrutionSender;
import com.icetec.iceintel.ui.activity.MainActivity;
import com.icetec.iceintel.ui.activity.SchemeActivity;

/**
 * Created by amo on 2014/12/20.
 * 模式列表的适配器，用以制定模式界面的列表
 */
public class SchemeAdapter extends DragAdapter {

    InstrutionSender instrutionSender;
    private ArrayList<Scheme> SchemeArrayList=new ArrayList<Scheme>();
    LayoutInflater layoutInflater;
    SchemeControlSqlHelper schemeSqlHelper;
    GroupControlSqlHelper groupControlSqlHelper;

    int arraySize;
    boolean funtionEnable;


    public  static SchemeAdapter schemeAdapter;


    public SchemeAdapter(){
        schemeAdapter=this;
        funtionEnable=true;
        schemeSqlHelper=new SchemeControlSqlHelper();
        groupControlSqlHelper=new GroupControlSqlHelper();
        this.SchemeArrayList.addAll(schemeSqlHelper.getAllDevice());
        layoutInflater=LayoutInflater.from(AppApplication.getShareInstance());
        instrutionSender=new InstrutionSender();

        //addAllStableId(SchemeArrayList);

    }

    public void clear() {
        SchemeArrayList.clear();
        notifyDataSetChanged();
    }

    public void updateListViewFromDb(){
        clear();
        this.SchemeArrayList.addAll(schemeSqlHelper.getAllDevice());
        //从数据库中更新 deviceArrayList
        notifyDataSetChanged();

    }

    public void saveAllDeviceReArrange(){
        schemeSqlHelper.saveAllDeviceReArrange(SchemeArrayList);
    }


    @Override
    public int getCount() {
        arraySize=SchemeArrayList.size();
        return  arraySize;
    }


    @Override
    public Scheme getItem(int position) {
        return SchemeArrayList.get(position);
    }


    @Override
    public long getItemId(int position) {
        return SchemeArrayList.get(position).getId();
    }

    @Override
    public List<Object> getList(){
        return (List)SchemeArrayList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        final  Scheme scheme=getItem(position);


        final LightListHolder lightListHolder;
        //(convertView==null){
        convertView=layoutInflater.inflate(R.layout.component_schemeitems,null);
        lightListHolder=new LightListHolder();

        lightListHolder.lightIconLeft=(ImageView)convertView.findViewById(R.id.lightlist_item_light_btn_left);
        lightListHolder.lightNameLeft=(ImageView)convertView.findViewById(R.id.lightlist_item_light_left_more);
        lightListHolder.lightnameLeftTv=(TextView)convertView.findViewById(R.id.lightlist_item_light_left_tv);
        lightListHolder.leftRely=(RelativeLayout)convertView.findViewById(R.id.lightlist_item_left);
        lightListHolder.schemeIconBottom=(ImageView)convertView.findViewById(R.id.lightlist_item_light_bottom);


        convertView.setTag(lightListHolder);
        //}
        //else
        //{
        // lightListHolder=(LightListHolder)convertView.getTag();
        //}



        lightListHolder.lightIconLeft.setImageDrawable(UniResAllocator.getSchemeResIDbyType(scheme.getType(), false));
        lightListHolder.schemeIconBottom .setImageDrawable(UniResAllocator.getSchemeResIDbyType(scheme.getType(), true));



//        lightListHolder.lightIconLeft.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                new AlertDialog.Builder(new ContextThemeWrapper(MainActivity.shareInstance, R.style.iceDialogTheme))
//                        .setItems(new String[] {AppApplication.getShareInstance().getString(R.string.delete_schema)},
//                                new DialogInterface.OnClickListener() {
//
//                                    public void onClick(DialogInterface dialog, int which) {
//
//                                        if(which==0){
//
//
//
//                                            schemeSqlHelper.deleteScheme(scheme);
//                                            updateListViewFromDb();
//                                        }
//
//
//                                        dialog.dismiss();
//                                    }
//                                }
//                        )
//                            // .setNegativeButton("取消", null)
//                        .show();
//                return  true;
//            }
//            });


        //lightListHolder.lightIconLeft.setLongClickable(funtionEnable);




//        lightListHolder.lightIconLeft.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//
//                if(event.getAction()==MotionEvent.ACTION_DOWN){
//                    if(funtionEnable) {
//                        instrutionSender.sendScheme(scheme);
//                    }
//                    Animation animationout=new AlphaAnimation(1.0f,0f);
//                    animationout.setDuration(500);
//                    lightListHolder.schemeIconBottom.startAnimation(animationout);
//                    return false;
//                }
//
//                if(event.getAction()==MotionEvent.ACTION_UP){
//                    return false;
//                }
//
//                return false;
//            }
//        });

        lightListHolder.lightIconLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(funtionEnable) {
                    instrutionSender.sendScheme(scheme);
                }
                Animation animationout=new AlphaAnimation(1.0f,0f);
                animationout.setDuration(500);
                lightListHolder.schemeIconBottom.startAnimation(animationout);
            }
        });


        lightListHolder.lightnameLeftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("click:", "nameTv");
                Intent intent = new Intent(MainActivity.shareInstance, SchemeActivity.class);
                intent.putExtra(SchemeActivity.ID, scheme.getId());
                MainActivity.shareInstance.startActivity(intent);
            }
        });



     /*   lightListHolder.lightnameLeftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });*/


        if(scheme.getName()!=null) {
            lightListHolder.lightnameLeftTv.setText(scheme.getName());

        }

        else {
            lightListHolder.lightnameLeftTv.setText(R.string.unnamed_scheme);
        }

        if(position == mHidePosition){
            convertView.setVisibility(View.INVISIBLE);
        }


        return convertView;

    }

    public  class LightListHolder{
        ImageView lightIconLeft;
        ImageView  lightNameLeft;
        ImageView schemeIconBottom;
        TextView lightnameLeftTv;
        RelativeLayout leftRely;
    }

    public void setModifyMode(boolean yes) {
        funtionEnable=!yes;

    }
}
